﻿using Styx.WoWInternals;
using System;

namespace LevelingHelper
{
    class Events
    {
        public static void AttachEvents()
        {
            Lua.Events.AttachEvent("LOOT_CLOSED", LootFinished);
            Lua.Events.AttachEvent("MAIL_CLOSED", MailboxFinished);
            Lua.Events.AttachEvent("GUILD_INVITE_REQUEST", GotInvitedToGuild);
        }

        public static void DetachEvents()
        {
            Lua.Events.DetachEvent("LOOT_CLOSED", LootFinished);
            Lua.Events.DetachEvent("MAIL_CLOSED", MailboxFinished);
            Lua.Events.DetachEvent("GUILD_INVITE_REQUEST", GotInvitedToGuild);
        }

        #region Inventory check Events

        public static void LootFinished(object sender, LuaEventArgs args)
        {
            if (!LHSettings.Instance.EnableInventoryCleanup) return;

            if (InventoryHandler.InventoryCheck == false)
            {
                InventoryHandler.InventoryCheck = true;
                Main.Log("We're gonna check inventory ASAP ! Stay tuned...");
            }
        }

        public static void MailboxFinished(object sender, LuaEventArgs args)
        {
            if (!LHSettings.Instance.EnableInventoryCleanup) return;

            if (InventoryHandler.InventoryCheck == false)
            {
                InventoryHandler.InventoryCheck = true;
                Main.Log("We're gonna check inventory ASAP ! Stay tuned...");
            }
        }

        #endregion

        #region Guild Events

        public static void GotInvitedToGuild(object sender, LuaEventArgs args)
        {
            if (!LHSettings.Instance.AutoAcceptGuildInvites) return;

            //var inviteAuthor = args.Args[0].ToString(); Useless for the moment, maybe later.
            var guildName = args.Args[1].ToString();
            var currentGuildLevel = Convert.ToUInt32(args.Args[2]);

            if (currentGuildLevel >= 25)
            {
                Main.Log(string.Format("{0} is a guild is level 25 ! let's get into this.", guildName));
                Lua.DoString("AcceptGuild()");
                Lua.DoString("StaticPopup_Hide(\"GUILD_INVITE_REQUEST\")");
                Lua.Events.DetachEvent("GUILD_INVITE_REQUEST", GotInvitedToGuild);
            }
        }

        #endregion

    }
}
